
import { _decorator, Animation, Button, Component, Label, Node, v3, Vec3 } from 'cc';
import EventHelper from 'db://cocos-ext/base/EventHelper';
import { UtilCocos } from 'db://cocos-ext/base/UtilCocos';
import { players } from 'yycore';

import SlotBetModule from './SlotBetModule';

const { ccclass, property } = _decorator;
// 按钮约定的摆放位置
const PLUS_POS: Vec3 = v3(125, 3);
const SUB_POS: Vec3 = v3(-125, 3);
// 锁摆放位置
const SUB_LOCK_POS: Vec3 = v3(-39, 30);
const PLUS_LOCK_POS: Vec3 = v3(39, 30);

/** 改变押注档位的按钮模型 */
@ccclass('SlotBtnChangeBet')
export default class SlotBtnChangeBet extends Component {

    // +-号按钮(由创建者绑定点击事件)
    @property(Button)
    public readonly btn: Button = null;

    @property(Node)
    private readonly iconPlus: Node = null;
    @property(Node)
    private readonly iconSub: Node = null;

    // 锁UI根节点
    @property(Node)
    private readonly lockNode: Node = null;

    @property(Label)
    private lvLabel: Label = null;

    @property(Animation)
    private unLockAnim: Animation = null;

    @property(Node)
    private lockIcon: Node = null;


    /** 是否加锁 */
    private get isUnlock(): boolean {
        return this._isUnlock;
    }
    private set isUnlock(b: boolean) {
        this._isUnlock = b;
        this.updateBtnStatus();
    }
    private _isUnlock: boolean = false;

    /** 按钮逻辑状态 */
    public get btnStatus(): boolean {
        return this._isBtnEnable;
    }
    public set btnStatus(b: boolean) {
        this._isBtnEnable = b;
        this.updateBtnStatus();
    }
    private _isBtnEnable: boolean = true;

    /** 是否是+号 */
    get isPlus(): boolean {
        return this._isPlus;
    }
    set isPlus(b: boolean) {
        this._isPlus = b;
        // 位置矫正
        this.node.setPosition(b ? PLUS_POS : SUB_POS);
        this.lockNode.setPosition(b ? PLUS_LOCK_POS : SUB_LOCK_POS);
        // icon
        this.iconPlus.active = b;
        this.iconSub.active = !b;
    }
    private _isPlus: boolean = true;


    protected onLoad(): void {
        // 默认不可用，并隐藏锁
        this.isUnlock = false;
        this.lockNode.active = false;
    }

    protected onDestroy(): void {
        this.btn.node.targetOff(this);
    }

    protected start(): void {
        this.refresh();
    }

    /** 按钮点击事件绑定 */
    set btnEvent(func: () => void) {
        this.btn.node.targetOff(this);
        this.btn.node.on(Button.EventType.CLICK, func, this);
    }

    private updateBtnStatus() {
        this.btn.interactable = this._isUnlock && this._isBtnEnable;
        UtilCocos.setNodeGray(this.btn.node, !this.btn.interactable, false);
    }

    public onExpChanged(): boolean {
        if (this.unLockAnim.node.active) return;
        let isLock = this.lockNode.active;
        this.refresh();
        if (isLock && !this.lockNode.active) {
            this.lockNode.active = true;
            this.playUnlockAnim()
            return true
        }
        return false
    }

    private playUnlockAnim() {
        this.lockIcon.active = false;
        this.unLockAnim.node.active = true;
        this.unLockAnim.play();
        this.unLockAnim.on(Animation.EventType.FINISHED, this.refresh, this, true);
        EventHelper.playAudio("unlock");
    }

    public refresh() {
        let nextUnlockLv = 0;
        let nextIdx = 0;
        const aryLen = SlotBetModule.lockChips.length;
        if (this.isPlus) {
            nextIdx = SlotBetModule.curUiBetLv + 1;
            if (nextIdx > aryLen - 1)
                nextIdx = 0;
        }
        else {
            nextIdx = SlotBetModule.curUiBetLv - 1;
            if (nextIdx < 0)
                nextIdx = aryLen - 1;
        }
        if (nextIdx > -1 && nextIdx < aryLen)
            nextUnlockLv = SlotBetModule.lockChips[nextIdx];
        this.setNextLevel(nextUnlockLv);
    }

    private setNextLevel(nextlevel: number) {
        this.unLockAnim.node.active = false;
        this.lockIcon.active = true;

        let exp = players.myself.getExpLevel();
        let bol = exp && exp.level >= nextlevel;
        this.lockNode.active = !bol;
        this.isUnlock = bol;
        this.lvLabel.string = nextlevel.toString();

        const betAryLen = SlotBetModule.betChips.length;
        const hideLock = SlotBetModule.curUiBetLv == 0 && !this.isPlus
            || (SlotBetModule.curUiBetLv == betAryLen - 1 && this.isPlus)
        if (hideLock) {
            this.lockNode.active = false;
            this.isUnlock = false;
        }
    }

}
